/**
 * @param {number[]} code
 * @param {number} k
 * @return {number[]}
 */
var decrypt = function (code, k) {
    if (k === 0) return new Array(code.length).fill(0)
    let winL = Math.abs(k)
    let i = 0, j = 0, sum = 0
    let res = []
    let len = code.length
    while(i < len) {
        sum += code[j]
        if((i + winL - 1) % len  === j) {
            let index
            if(k > 0) {
                index = i - 1 < 0 ? len - 1 : i - 1
            }else if(k < 0) {
                index = j + 1 === len ? 0 : j + 1
            }
            res[index]  = sum
            sum -= code[i]
            i++;
        }
        j = (j+1) % len
    }
    return res
};
console.log(decrypt([5, 7, 1, 4], 3));
console.log(decrypt([2, 4, 9, 3], -2));
